package com.gpipe.tools;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


/*
 * 
 * 参数：point=112:89  pointList=112:89;112:89;112:89
 * 结果：1：在范围内；-1：不在范围内；0：缺少数据，无法判断
 * 用法：evaluate(112:89,112:89;112:89;112:89)
 * 参考：http://www.2cto.com/kf/201410/341911.html
 */
public class PointIsIn {
	final private String deli1 = ":";
	final private String deli2 = "-";

	public String evaluate(String point, String pointList) throws IOException {
		// int len = pointList.split(deli2).length;

		// System.out.println("----len:"+len);

		if (point != null && !point.equals("")) {
			if (pointList.split(deli2).length >= 3) {
				// System.out.println("----len:1");
				return getResult(point, pointList);
			} else {
				// System.out.println("----len:0");
				return "0";
			}

		} else {
			// System.out.println("----len:0");
			return "0";
		}

	}

	private String getResult(String point, String pointList) {
		// 用户所在经纬度
		Point pointUser = new Point();
		// 所圈定范围的边界点
		List<Point> pointBorder = new ArrayList<Point>();

		// 规整输入的参数，并持久化
		pointUser = parsePoint(point);
		String[] points = pointList.split(deli2);
		for (String tmpPoi : points) {
			pointBorder.add(parsePoint(tmpPoi));
		}

		// 开始判断点是否在范围内
		String result = isIn(pointUser, pointBorder);

		return result;

	}

	/*
	 * 功能：判断点是否在范围内
	 */
	public String isIn(Point pointUser, List<Point> pointBorder) {
		boolean boolres = isInPolygon(pointUser, pointBorder,
				pointBorder.size());

		if (boolres) {
			return "1";
		} else {
			return "-1";
		}
	}

	private boolean isInPolygon(Point point, List<Point> pointBorder, int n) {
		int nCross = 0;
		for (int i = 0; i < n; i++) {
			Point p1 = pointBorder.get(i);
			Point p2 = pointBorder.get((i + 1) % n);
			// 求解 y=p.y 与 p1 p2 的交点
			// p1p2 与 y=p0.y平行
			if (p1.y == p2.y)
				continue;
			// 交点在p1p2延长线上
			if (point.y < Math.min(p1.y, p2.y))
				continue;
			// 交点在p1p2延长线上
			if (point.y >= Math.max(p1.y, p2.y))
				continue;
			// 求交点的 X 坐标
			double x = (double) (point.y - p1.y) * (double) (p2.x - p1.x)
					/ (double) (p2.y - p1.y) + p1.x;
			// 只统计单边交点
			if (x > point.x)
				nCross++;
		}
		return (nCross % 2 == 1);
	}

	/*
	 * 功能：对String的点进行解析，持久化point类里面
	 */
	private Point parsePoint(String point) {

		String[] tmp = point.split(deli1);

		Point result = new Point();
		result.setPoint(Double.parseDouble(tmp[0]), Double.parseDouble(tmp[1]));
		return result;
	}
	

	    

	public static void main(String[] args) throws IOException {
		PointIsIn aa = new PointIsIn();
		// 在里面
		 String tmp1 =
		 aa.evaluate("113.31729:23.14138","113.309268:23.143511-113.323126:23.143739-113.322785:23.136176-113.309687:23.136658");

		// 在外面
//		String tmp1 = aa
//				.evaluate(
//						"113.309268:23.143511",
//						"113.322785:23.136176-113.328913:23.135858-113.337174:23.138218-113.338088:23.128739-113.322707:23.129842");

		System.out.println(tmp1);

		// -------------------------
		
		
//		File file = new File("E:\\testLog\\jw_test_file\\basestation_regiodId.txt");
//        FileWriter fw = null;
//        BufferedWriter writer = null;
//        fw = new FileWriter(file);
//        writer = new BufferedWriter(fw);
//            
//		
//		String _fileName = "E:\\testLog\\jw_test_file\\basestation.txt";
//		Map<String, String> records = new HashMap<String, String>();
//		String[] regionConf = new String[]{
//				"1|113.309268:23.143511-113.323126:23.143739-113.322785:23.136176-113.309687:23.136658",
//				"2|113.323126:23.143739-113.340753:23.14412-113.337174:23.138218-113.328913:23.135858-113.322785:23.136176",
//				"3|113.322785:23.136176-113.328913:23.135858-113.337174:23.138218-113.338088:23.128739-113.322707:23.129842",
//				"4|113.309687:23.136658-113.322785:23.136176-113.322707:23.129842-113.313722:23.130452-113.309877:23.130452"
//		};
//		
//		
//		BufferedReader reader = null;
//		try {
//			reader = new BufferedReader(new InputStreamReader(
//					new FileInputStream(_fileName)));
//		} catch (FileNotFoundException e) {
//			e.printStackTrace();
//		}
//		String line = null;
//		while ((line = reader.readLine()) != null) {
//			String[] field = line.split("\\|", -1); 
//			String key = field[4]+":"+field[5];
//			String val = field[0]+"|"+field[1]+"|"+field[2]+"|"+field[3];
//			
//			for(String str:regionConf){
//				String[] strs = str.split("\\|",-1);
//				String regionId = strs[0];
//				String border = strs[1];
//				
//				String tmp = aa.evaluate(key,border);
//				if(tmp.equals("1")){
//					String tt =line+"|"+regionId;
//					System.out.println(tt);
//					writer.write(tt);
//	                writer.newLine();//换行
//				}
//			}
//			
//			
//		}
//		writer.flush();
//		writer.close();
//        fw.close();

	}
}
